home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 173 / 173.xpi / chrome / gm-notifier.jar / content / gm-notifier / gm-sm-uninstaller.js < prev    next >
Text File  |  2009-09-28  |  5KB  |  130 lines

  1. // Uninstall ******* 
  2. // big HACK
  3. function gm_notifier_uninstall(){
  4.   var confirmUninstall = confirm("The notifier will be uninstalled after Mozilla is restarted. \n\nDo you want to uninstall?");
  5.   
  6.   if (!confirmUninstall) // user choose to cancel
  7.     return;
  8.  
  9.   // remove the UI
  10.   
  11.   //remove the overlay entry in overlays.rdf
  12.  
  13.   var CONTAINER_NAME  = "chrome://navigator/content/navigator.xul";
  14.   var NODE_NAME       = "chrome://gm-notifier/content/gm-sm-overlay.xul";
  15.  
  16.   var dirServ = Components.classes['@mozilla.org/file/directory_service;1'].createInstance();
  17.   dirServ = dirServ.QueryInterface(Components.interfaces.nsIProperties);
  18.   var processDir = dirServ.get("ProfD", Components.interfaces.nsIFile);
  19.   processDir.append("chrome");
  20.   processDir.append("overlayinfo");
  21.   processDir.append("navigator");
  22.   processDir.append("content");
  23.   processDir.append("overlays.rdf");
  24.  
  25.   removeRDFContainer(processDir, CONTAINER_NAME, NODE_NAME);
  26.  
  27.   // remove chrome.rdf entries
  28.  
  29.   CONTAINER_NAME = 'urn:mozilla:package:root';
  30.   NODE_NAME = 'urn:mozilla:package:gm-notifier';
  31.   processDir = dirServ.get("ProfD", Components.interfaces.nsIFile);
  32.   processDir.append("chrome");
  33.   processDir.append("chrome.rdf");
  34.  
  35.   removeRDFContainer(processDir, CONTAINER_NAME, NODE_NAME);
  36.  
  37.   // remove the installed-chrome.txt entries - thanks to HJ for the code
  38.   /*
  39.   processDir = dirServ.get("ProfD", Components.interfaces.nsIFile);
  40.   processDir.append("chrome");
  41.   processDir.append("installed-chrome.txt");
  42.   
  43.   var entries = ReadFrom(processDir);
  44.   entries = entries.replace(new RegExp('[^\\n\\r]+'+"resource:/chrome/gm-notifier.jar!/content/"+'[\\n\\r]+', 'g'), '');
  45.   WriteTo(processDir, entries); 
  46.   */
  47.   function removeRDFContainer(fileRef, containerName, nodeName){
  48.     var C                       = Components;
  49.     const RDF_SRVC_PROG_ID      = '@mozilla.org/rdf/rdf-service;1';
  50.     const RDF_CONTAINER_PROG_ID = '@mozilla.org/rdf/container;1';
  51.     const RDF_DS_PROG_ID        = '@mozilla.org/rdf/datasource;1?name=xml-datasource';
  52.         
  53.     if (!fileRef.exists()) {
  54.         return;
  55.     }
  56.  
  57.     var ioService = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
  58.  
  59.     var myFileUrl = null;
  60.     
  61.     // Interface change - ns7 (mozilla 1.0) needs nsIIOService, later (mozilla 1.2) needs nsIFileProtocolHandler
  62.     if(Components.interfaces.nsIFileProtocolHandler){
  63.       var fileHandler = ioService.getProtocolHandler("file").QueryInterface(Components.interfaces.nsIFileProtocolHandler);  
  64.       myFileUrl = fileHandler.getURLSpecFromFile(fileRef);
  65.     } else {
  66.       myFileUrl = ioService.getURLSpecFromFile(fileRef);    
  67.     }  
  68.   
  69.     try {
  70.       var rdf   = C.classes
  71.                   [RDF_SRVC_PROG_ID].getService(C.interfaces.nsIRDFService);
  72.  
  73.       var ds    = rdf.GetDataSource(myFileUrl);
  74.   
  75.       var rem   = ds.QueryInterface(C.interfaces.nsIRDFRemoteDataSource);
  76.       if (!rem.loaded) {
  77.         rdf.UnregisterDataSource(ds);
  78.         ds        = C.classes[RDF_DS_PROG_ID].
  79.                     getService(C.interfaces.nsIRDFDataSource);
  80.         rem       = ds.QueryInterface(C.interfaces.nsIRDFRemoteDataSource);
  81.         rem.Init(myFileUrl);
  82.       }
  83.       rem.Refresh(true)
  84.       var rs  = rdf.GetResource(containerName);
  85.       var container = C.classes[RDF_CONTAINER_PROG_ID].
  86.                            getService(C.interfaces.nsIRDFContainer);
  87.       container.Init(rem, rs);
  88.  
  89.       var el = rdf.GetLiteral(nodeName); // remove if it's a literal
  90.       container.RemoveElement(el, true);
  91.       el = rdf.GetResource(nodeName); // remove it it's a resource
  92.       container.RemoveElement(el, true);
  93.       rem.Flush()
  94.       rem.Refresh(true)
  95.     } catch (e) {
  96.       return;
  97.     }
  98.   }
  99.   
  100.   function ReadFrom(aFile) 
  101.     {
  102.         var stream = Components.classes['@mozilla.org/network/file-input-stream;1'].createInstance(Components.interfaces.nsIFileInputStream);
  103.         stream.init(aFile, 1, 0, false); // open as "read only"
  104.  
  105.         var scriptableStream = Components.classes['@mozilla.org/scriptableinputstream;1'].createInstance(Components.interfaces.nsIScriptableInputStream);
  106.         scriptableStream.init(stream);
  107.  
  108.         var fileSize = scriptableStream.available();
  109.         var fileContents = scriptableStream.read(fileSize);
  110.  
  111.         scriptableStream.close();
  112.         stream.close();
  113.  
  114.         return fileContents;
  115.   }
  116.   function WriteTo(aFile, aContent) 
  117.     {
  118.         if (aFile.exists()) aFile.remove(true);
  119.         aFile.create(aFile.NORMAL_FILE_TYPE, 0666);
  120.  
  121.         var stream = Components.classes['@mozilla.org/network/file-output-stream;1'].createInstance(Components.interfaces.nsIFileOutputStream);
  122.         stream.init(aFile, 2, 0x200, false); // open as "write only"
  123.  
  124.         stream.write(aContent, aContent.length);
  125.  
  126.         stream.close();
  127.     }  
  128. }
  129.  
  130.